package com.flyduck.vueshop.product.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flyduck.vueshop.common.core.utils.BeanUtils;
import com.flyduck.vueshop.common.core.utils.StringUtils;
import com.flyduck.vueshop.common.mybatis.utils.PageUtils;
import com.flyduck.vueshop.product.admin.service.AdminAdService;
import com.flyduck.vueshop.product.admin.vo.*;
import com.flyduck.vueshop.product.entity.AppAd;
import com.flyduck.vueshop.product.mapper.AppAdMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @program: vueshop
 * @description:
 * @author: flyduck
 * @create: 2024-06-03 21:44
 **/
@Service
public class AdminAdServiceImpl implements AdminAdService {

    @Resource
    private AppAdMapper appAdMapper;

    @Override
    public Page<AdminAdRespVO> getAdPage(Page page, AdminAdQueryReqVO adminAdQueryReqVO) {
        Page<AppAd> appAdPage = appAdMapper.selectPage(page,
                new LambdaQueryWrapper<AppAd>()
                        .like(StringUtils.isNotBlank(adminAdQueryReqVO.getName()), AppAd::getName, adminAdQueryReqVO.getName())
                        .orderByDesc(AppAd::getCreated)
        );

        return PageUtils.convert(appAdPage, AdminAdRespVO.class);
    }

    @Override
    public AdminAdDetailsRespVO getAdDetailsById(Long id) {
        AppAd appAd = appAdMapper.selectById(id);
        return BeanUtils.toBean(appAd, AdminAdDetailsRespVO.class);
    }

    @Override
    public void saveAd(AdminAdSaveReqVO adminAdSaveReqVO) {
        AppAd appAd = BeanUtils.toBean(adminAdSaveReqVO, AppAd.class);
        appAd.setCreated(LocalDateTime.now());
        appAdMapper.insert(appAd);
    }

    @Override
    public void updateAd(AdminAdUpdateReqVO adminAdUpdateReqVO) {
        AppAd appAd = BeanUtils.toBean(adminAdUpdateReqVO, AppAd.class);
        appAd.setUpdated(LocalDateTime.now());
        appAdMapper.updateById(appAd);
    }

    @Override
    public void batchDeleteByIds(List<Long> ids) {
        appAdMapper.deleteBatchIds(ids);
    }
}
